package JavaSe;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}12:18
 */
public class ReflectTest {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        String s ="ss";
        printClassInfo(s);
        A a = new A();
        Class c = a.getClass();
        Method method = c.getMethod("add",new Class[]{int.class,int.class});
        method.invoke(a,10,10);
    }
    public static void printClassInfo(Object o){
        Class c = o.getClass();
        System.out.println("类的名称"+c.getName());

        Method[] methods = c.getMethods();

        for (int i = 0; i < methods.length;i++){
            Class returnType = methods[i].getReturnType();
            System.out.println(methods[i].getName());

            Class[] parameterTypes=methods[i].getParameterTypes();
            for (Class class1:parameterTypes){
                System.out.println(class1.getName());
            }
            System.out.println("--------");
        }

        Field[] fileds=c.getDeclaredFields();

        for(Field f:fileds){
            //获取成员变量的类型
            Class filedType=f.getType();
            System.out.println(filedType.getName()+" "+f.getName());
        }

        Constructor[] constructors=c.getDeclaredConstructors();
        for (Constructor con:constructors){
            System.out.print(con.getName()+"(");

            Class[] typeParas=con.getParameterTypes();
            for (Class class1:typeParas){
                System.out.print(class1.getName()+" ,");
            }
            System.out.println(")");
        }
    }

    static class A{
        public void add(int a,int b){
            System.out.println(a+b);
        }
    }


}
